home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Mail For Me / Mail4ME.jar / de / trantor / mail / Message.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-10-21  |  8.9 KB  |  328 lines

  1. package de.trantor.mail;
  2.  
  3. import java.util.Calendar;
  4. import java.util.Random;
  5. import java.util.TimeZone;
  6. import java.util.Vector;
  7.  
  8. public class Message {
  9.    private Vector lines = new Vector();
  10.    private int headerSize = 0;
  11.  
  12.    public Message() {
  13.       this.lines.addElement("");
  14.    }
  15.  
  16.    public Message(String from, String to, String subject) {
  17.       this.lines.addElement("");
  18.       if (from != null) {
  19.          this.addHeaderLine("From: " + from);
  20.          String s = getMachineAddress(from);
  21.          this.addHeaderLine("Message-ID: <" + getRandomString() + "." + s + ">");
  22.       } else {
  23.          this.addHeaderLine("Message-ID: <" + getRandomString() + ">");
  24.       }
  25.  
  26.       if (to != null) {
  27.          this.addHeaderLine("To: " + to);
  28.       }
  29.  
  30.       if (subject != null) {
  31.          this.addHeaderLine("Subject: " + subject);
  32.       }
  33.  
  34.       this.addHeaderLine("Date: " + getCanonicalDate(Calendar.getInstance(), TimeZone.getDefault()));
  35.    }
  36.  
  37.    public String getHeaderLine(int index) throws ArrayIndexOutOfBoundsException {
  38.       if (index >= 0 && index < this.headerSize) {
  39.          return (String)this.lines.elementAt(index);
  40.       } else {
  41.          throw new ArrayIndexOutOfBoundsException(index);
  42.       }
  43.    }
  44.  
  45.    public String getHeaderName(int index) throws ArrayIndexOutOfBoundsException {
  46.       return getStringName(this.getHeaderLine(index));
  47.    }
  48.  
  49.    public String getHeaderValue(int index) throws ArrayIndexOutOfBoundsException {
  50.       return getStringValue(this.getHeaderLine(index));
  51.    }
  52.  
  53.    public int getHeaderIndex(String name, int startIndex) {
  54.       String lowerName = name.toLowerCase();
  55.  
  56.       for(int i = startIndex; i < this.headerSize; ++i) {
  57.          String s = this.getHeaderName(i);
  58.          if (s != null && s.toLowerCase().equals(lowerName)) {
  59.             return i;
  60.          }
  61.       }
  62.  
  63.       return -1;
  64.    }
  65.  
  66.    public int getHeaderIndex(String name) {
  67.       return this.getHeaderIndex(name, 0);
  68.    }
  69.  
  70.    public String getHeaderValue(String name) {
  71.       return this.getHeaderValue(name, (String)null);
  72.    }
  73.  
  74.    public String getHeaderValue(String name, String def) {
  75.       int i = this.getHeaderIndex(name);
  76.       return i == -1 ? def : this.getHeaderValue(i);
  77.    }
  78.  
  79.    public String[] getAllHeaderValues(String name) {
  80.       Vector lines = new Vector();
  81.  
  82.       for(int i = this.getHeaderIndex(name); i != -1; i = this.getHeaderIndex(name, i + 1)) {
  83.          lines.addElement(this.getHeaderValue(i));
  84.       }
  85.  
  86.       String[] result = new String[lines.size()];
  87.  
  88.       for(int j = 0; j < lines.size(); ++j) {
  89.          result[j] = (String)lines.elementAt(j);
  90.       }
  91.  
  92.       return result;
  93.    }
  94.  
  95.    public void setHeaderLine(int index, String line) {
  96.       if (index >= 0 && index < this.headerSize) {
  97.          this.lines.setElementAt(line, index);
  98.       } else {
  99.          throw new ArrayIndexOutOfBoundsException(index);
  100.       }
  101.    }
  102.  
  103.    public int getHeaderLineCount() {
  104.       return this.headerSize;
  105.    }
  106.  
  107.    public int addHeaderLine(String line) {
  108.       this.lines.insertElementAt(line, this.headerSize);
  109.       return this.headerSize++;
  110.    }
  111.  
  112.    public void insertHeaderLine(int index, String line) {
  113.       if (index >= 0 && index <= this.headerSize) {
  114.          this.lines.insertElementAt(line, index);
  115.          ++this.headerSize;
  116.       } else {
  117.          throw new ArrayIndexOutOfBoundsException(index);
  118.       }
  119.    }
  120.  
  121.    public void removeHeaderLine(int index) {
  122.       if (index >= 0 && index < this.headerSize) {
  123.          this.lines.removeElementAt(index);
  124.          --this.headerSize;
  125.       } else {
  126.          throw new ArrayIndexOutOfBoundsException(index);
  127.       }
  128.    }
  129.  
  130.    public void setHeaderValue(String name, String value) {
  131.       int i = this.getHeaderIndex(name);
  132.       if (i == -1) {
  133.          this.addHeaderLine(name + ": " + value);
  134.       } else {
  135.          this.setHeaderLine(i, name + ": " + value);
  136.       }
  137.  
  138.    }
  139.  
  140.    public String getBodyLine(int index) {
  141.       if (index >= 0 && index < this.getBodyLineCount()) {
  142.          return (String)this.lines.elementAt(this.headerSize + index + 1);
  143.       } else {
  144.          throw new ArrayIndexOutOfBoundsException(index);
  145.       }
  146.    }
  147.  
  148.    public void setBodyLine(int index, String line) {
  149.       if (index >= 0 && index < this.getBodyLineCount()) {
  150.          this.lines.setElementAt(line, this.headerSize + index + 1);
  151.       } else {
  152.          throw new ArrayIndexOutOfBoundsException(index);
  153.       }
  154.    }
  155.  
  156.    public int getBodyLineCount() {
  157.       return this.lines.size() - this.headerSize - 1;
  158.    }
  159.  
  160.    public int addBodyLine(String line) {
  161.       this.lines.addElement(line);
  162.       return this.getBodyLineCount() - 1;
  163.    }
  164.  
  165.    public void insertBodyLine(int index, String line) {
  166.       if (index >= 0 && index <= this.getBodyLineCount()) {
  167.          this.lines.insertElementAt(line, this.headerSize + index + 1);
  168.       } else {
  169.          throw new ArrayIndexOutOfBoundsException(index);
  170.       }
  171.    }
  172.  
  173.    public void removeBodyLine(int index) {
  174.       if (index >= 0 && index < this.getBodyLineCount()) {
  175.          this.lines.removeElementAt(this.headerSize + index + 1);
  176.       } else {
  177.          throw new ArrayIndexOutOfBoundsException(index);
  178.       }
  179.    }
  180.  
  181.    public static String getMachineAddress(String address) {
  182.       int p = address.indexOf(60);
  183.       int q = address.indexOf(62, p + 1);
  184.       if (p != -1 && q != -1) {
  185.          return address.substring(p + 1, q);
  186.       } else {
  187.          p = address.indexOf(40);
  188.          q = address.indexOf(41, p + 1);
  189.          if (p != -1 && q != -1) {
  190.             return address.substring(0, p).trim();
  191.          } else {
  192.             p = address.indexOf(34);
  193.             q = address.indexOf(34, p + 1);
  194.             return p != -1 && q != -1 ? address.substring(0, p).trim() : address;
  195.          }
  196.       }
  197.    }
  198.  
  199.    public static String getDisplayAddress(String address) {
  200.       int p = address.indexOf(34);
  201.       int q = address.indexOf(34, p + 1);
  202.       if (p != -1 && q != -1) {
  203.          return address.substring(p + 1, q);
  204.       } else {
  205.          p = address.indexOf(40);
  206.          q = address.indexOf(41, p + 1);
  207.          if (p != -1 && q != -1) {
  208.             return address.substring(p + 1, q);
  209.          } else {
  210.             p = address.indexOf(60);
  211.             q = address.indexOf(62, p + 1);
  212.             return p != -1 && q != -1 ? address.substring(0, p).trim() : "";
  213.          }
  214.       }
  215.    }
  216.  
  217.    public static String getCanonicalAddress(String address) {
  218.       int p = address.indexOf(60);
  219.       int q = address.indexOf(62, p + 1);
  220.       if (p != -1 && q != -1) {
  221.          return address.substring(p, q + 1);
  222.       } else {
  223.          p = address.indexOf(40);
  224.          q = address.indexOf(41, p + 1);
  225.          if (p != -1 && q != -1) {
  226.             return "<" + address.substring(0, p) + ">";
  227.          } else {
  228.             p = address.indexOf(34);
  229.             q = address.indexOf(34, p + 1);
  230.             return p != -1 && q != -1 ? "<" + address.substring(0, p) + ">" : "<" + address + ">";
  231.          }
  232.       }
  233.    }
  234.  
  235.    private static String intToStr(int value, int length) {
  236.       String result = Integer.toString(value);
  237.       result = "0000".substring(result.length(), length) + result;
  238.       return result;
  239.    }
  240.  
  241.    public static String getCanonicalDate(Calendar calendar, TimeZone timezone) {
  242.       String monthNames = "JanFebMarAprMayJunJulAugSepOctNovDec";
  243.       String dayNames = "SunMonTueWedThuFriSatSun";
  244.       int year = calendar.get(1);
  245.       int month = calendar.get(2);
  246.       int day = calendar.get(5);
  247.       int weekday = calendar.get(7) - 1;
  248.       int hour = calendar.get(11);
  249.       int minute = calendar.get(12);
  250.       int second = calendar.get(13);
  251.       String result = dayNames.substring(3 * weekday, 3 * weekday + 3) + ", " + intToStr(day, 2) + " " + monthNames.substring(3 * month, 3 * month + 3) + " " + intToStr(year, 4) + " " + intToStr(hour, 2) + ":" + intToStr(minute, 2) + ":" + intToStr(second, 2);
  252.       if (timezone != null) {
  253.          int offset = timezone.getRawOffset() / 1000;
  254.          if (timezone.useDaylightTime()) {
  255.             offset += 3600;
  256.          }
  257.  
  258.          String name;
  259.          if (offset >= 0) {
  260.             name = " GMT+";
  261.          } else {
  262.             name = " GMT-";
  263.             offset = -offset;
  264.          }
  265.  
  266.          result = result + name + intToStr(offset / 3600, 2) + intToStr(offset % 3600, 2);
  267.       }
  268.  
  269.       return result;
  270.    }
  271.  
  272.    public static String[] getStringElements(String s) {
  273.       Vector temp = new Vector();
  274.       int len = s.length();
  275.  
  276.       int q;
  277.       for(int p = 0; p < len; p = q + 1) {
  278.          q = p;
  279.  
  280.          for(boolean quote = false; q < len && (s.charAt(q) != ';' || quote); ++q) {
  281.             if (s.charAt(q) == '"') {
  282.                quote = !quote;
  283.             }
  284.          }
  285.  
  286.          temp.addElement(s.substring(p, q).trim());
  287.       }
  288.  
  289.       String[] result = new String[temp.size()];
  290.  
  291.       for(int i = 0; i < temp.size(); ++i) {
  292.          result[i] = (String)temp.elementAt(i);
  293.       }
  294.  
  295.       return result;
  296.    }
  297.  
  298.    public static String getStringName(String s) {
  299.       int p = s.indexOf(58);
  300.       return p == -1 ? null : s.substring(0, p);
  301.    }
  302.  
  303.    public static String getStringValue(String s) {
  304.       int p = s.indexOf(58);
  305.       String value;
  306.       if (p == -1) {
  307.          value = s;
  308.       } else {
  309.          value = s.substring(p + 1);
  310.       }
  311.  
  312.       value = value.trim();
  313.       if (value.length() > 1 && value.charAt(0) == '"' && value.charAt(value.length() - 1) == '"') {
  314.          value = value.substring(1, value.length() - 1);
  315.       }
  316.  
  317.       return value;
  318.    }
  319.  
  320.    public static String getRandomString() {
  321.       return Long.toString(System.currentTimeMillis(), 36) + "." + Integer.toString(Math.abs((new Random()).nextInt()), 36);
  322.    }
  323.  
  324.    Vector getLines() {
  325.       return this.lines;
  326.    }
  327. }
  328.